package managers;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;

import dataBase.magaers.XsltDBManager;

public class XsltManager {
	public static boolean translate(Document inDoc, String xslId, OutputStream out)  {
		try{
			
		TransformerFactory tFactory = TransformerFactory.newInstance();
		String xsl = null;
		try {
			xsl = XsltDBManager.getInstance().getXsl(xslId);
		} catch (SQLException e) {
			return false;
		}
		if (xsl == null){ 
			return false;
		}
		InputStream is =  new ByteArrayInputStream(xsl.getBytes());
		Transformer transformer	= tFactory.newTransformer(new StreamSource(is)); 
		StreamResult sResult = new StreamResult(out);
		transformer.transform(new DOMSource(inDoc), sResult);
		return true;
		}
		catch(TransformerException e){
			return false;
		}
				
	}
}
